<?php
/*
# ------------------------------------------------------------------------
# JA Job Board Package
# ------------------------------------------------------------------------
# Copyright (C) 2004-2010 JoomlArt.com. All Rights Reserved.
# @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
# Author: JoomlArt.com
# Websites: http://www.joomlart.com - http://www.joomlancers.com.
# ------------------------------------------------------------------------
*/

jimport( 'joomla.application.component.view' );

/**
 * @package Joomla
 * @subpackage JobBoard
 */
class JobBoardViewJAResumes extends JView
{
    /**
     * Display the view
     */
    function display($tmpl = null){
    	
		$Itemid = JRequest::getVar('Itemid');
		if(!$Itemid){
			$Itemid = get_Itemid(array('option'=>'com_jajobboard', 'view'=>'jaresumes', 'layout'=>'jalist'));
		}
	  	$this->assignRef('Itemid', $Itemid);
	  
    	$title = getPageTitleFromMenu();
    	$config = new JConfig();
		$document = JFactory::getDocument();
       	switch ($this->getLayout())
  		{  	  					
  			case 'jaform':	
  				$this->edit();  
  				if (!$title)
  				{
					if ($this->item->title) 
						$title = $this->item->title;
					else 
						$title = JText::_('Create new job seeker');
  				}
				$document->setTitle($title.' - '.$config->sitename);				  				
  				break;
  			case 'myresumes':
  			case 'jaview':
  				$this->multi_view();
  				if (!$title)
  				{
					$title = JText::_('Job seeker');
					if (count($this->items)==1){
						$user = JFactory::getUser($this->items[0]->user_id);
						$title .= ' '. $user->name;
						$title .= ' - '. $this->items[0]->title;
						
						$jobs = $this->items[0];
					    $jobs->title = $this->items[0]->title;
					    $jobs->job_detail = "";					    
						if ( ! setPageTitle(array("res_details_title", "res_details_keyword", "res_details_description"), $jobs, $title ) )
							$document->setTitle($title.' - '.$config->sitename);
					}
  				}
				
  				break;
  				
  			case 'jaemployerlist':
  				$this->setLayout('jaemployerlist');
  				$this->displayList();
  				if (!$title)
					$title = JText::_('Job seeker').'/'.JText::_('Company favourite');
				$document->setTitle($title.' - '.$config->sitename);
  				break;
  			case 'userdetails':  			   
  			    break;
  			case 'myresumes':		
  			case 'viewdetail':
  				$this->setLayout('viewdetail');
  				$this->view();
  				$this->multi_view();
  				if (!$title)
					$title = JText::_('Job seeker');
				$document->setTitle($title.' - '.$config->sitename);
  				break;
  			case 'multi_view':
        		$this->multi_view();
  				if (!$title)
					$title = JText::_('Job seeker');
				$document->setTitle($title.' - '.$config->sitename);
        		break;
  			case 'filter_resume':
  				$this->filter_resume();
  				$this->setLayout('jalist');
  				$this->multi_view();
  				if (!$title)
					$title = JText::_('Search Job Seeker');
				$document->setTitle($title.' - '.$config->sitename);
  				break;
  			case 'jalist':
  			default:
  				$this->setLayout('jalist');
  				$this->displayList();  				
				if (!$title){
					$title = JText::_('Job seeker');
					if ( ! setPageTitle(array("res_list_title", "res_list_keyword", "res_list_description"), null, $title) )
						$document->setTitle($title.' - '.$config->sitename);
				}
  		}
  		
        parent::display($tmpl);
  	}
  	
  	function displayList()
  	{
		if (!checkPermission('can_search_resume'))
  			checkPermission('view_list_resumes');		
        		
  		global $mainframe, $option, $jbconfig;      		        
  		 // get data items
        $model = &$this->getModel();               
        
        $where_more = '';
        $user = JFactory::getUser();
        if($this->getLayout()=='jaemployerlist'){
        	
        	$db = JFactory::getDBO();
        	
        	$sql = "select item_id from #__ja_shortlist where user_id='$user->id' and listtype='ja_resumes'";
        	$db->setQuery($sql);
        	$result = $db->loadResultArray();
        	
        	if($result){
        		$items_id = implode(',', $result);
        		$where_more .= " and a.id in ($items_id)";
        	}
        	else 
        		$where_more .= " and a.id = 0";
        	
        	$this->setLayout('jalist');
        }
        
        $where_more .= ' and a.published=1';
        $where_more .= ' and a.approved=1';
        
        $limitstart = JRequest::getInt('limitstart', 0);
		$limit		= $mainframe->getUserStateFromRequest( $option.'.jaresumes.limit',	'limit',	$jbconfig['posts']->get('display_num', 20),	'int' );
				
       	$items = $model->getItems($where_more, $limit, $limitstart); 
       	$item_notes = $model->getItemNotes($items);      	
       	
       	$fieldEditList = $model->getEditFieldList(' and f.frontend_hidden=0');
       	$fieldList = $model->getFieldList();

		$pagination = &$this->get('Pagination');       
        
        $lists = $this->_getViewLists($fieldEditList);  
        
        $ordering = true;      
        $this->assignRef('items', $items);
        $this->assignRef('item_notes', $item_notes);
        $this->assignRef('pagination', $pagination);
        $this->assignRef('fieldlist', $fieldList);
        $this->assignRef('lists', $lists);
        $this->assignRef('ordering', $ordering );
                
  	}
  
  	/**
  	* Display edit form
  	*/
  	function edit($postback=false)
  	{  		  		
  		checkPermission('post_resume');
    	$item = $this->get('Item');
    	// Make sure you are logged in and have the necessary access rights
        if($postback){
            $post    = JRequest::get('request', JREQUEST_ALLOWHTML);
            $item->bind($post);
        }
        
        $fieldList = $this->get('EditFieldList');

        $this->assignRef('item', $item);
        $this->assignRef('fieldlist', $fieldList);

        $this->assignRef('lists', $lists);
        
    }
    
    function view(){
		$user = JFactory::getUser();
    	
    	$item = $this->get('ItemView');
    	if (isset($item))
	    	if ($item->user_id!=$user->id)
	    		checkPermission('view_resume_details');
        
        // Check if user has a resume
        if (isset($item))
	        if(!$item->id){          
	            return;
	        }
        
        $mail_postback = JRequest::getVar('mail_postback',false);
        $mail_postback_jobseeker = JRequest::getVar('mail_postback_jobseeker',false);
    	if ($mail_postback || $mail_postback_jobseeker)
    	{
    		$mail_data = array();
	    	$mail_data['youremail'] = JRequest::getVar('youremail');
	    	$mail_data['friendemail'] = JRequest::getVar('friendemail');
	    	$mail_data['subject'] = JRequest::getVar('subject');
	    	$mail_data['message'] = JRequest::getVar('message');
    	}                      
    	$this->assignRef('mail', $mail_data);
        
    	$db = JFactory::getDBO();
    	$query = "SELECT * FROM #__ja_notes WHERE user_id=".(int)$user->id." and item_id=".(int)$item->id." and note_type='ja_resumes'";
    	$db->setQuery($query);
    	$note = $db->loadObject();
    	if (!$note)
    	{
    		require_once(JPATH_COMPONENT_ADMINISTRATOR.DS.'tables'.DS.'ja_notes.php');
			$note = new JobBoardTableJA_Notes($db);
    	}
    	$this->assignRef('note', $note);
    	
    	
        $fieldList = $this->get('EditFieldList');
        $fromsearch = JRequest::getVar('fromsearch');
        
        $this->assignRef('item', $item);
        $this->assignRef('fieldlist', $fieldList);
        $this->assignRef('fromsearch', $fromsearch);
        
    }
    
    
	function &_getViewLists($fieldList){
		global $mainframe, $option;

		$db = &JFactory::getDBO();
		
        
		$lists['order']		= $mainframe->getUserStateFromRequest( $option.".jaresumes.filter_order",		'filter_order',		'a.created_date',	'cmd' );
		$lists['order_Dir']	= $mainframe->getUserStateFromRequest( $option.".jaresumes.filter_order_Dir",	'filter_order_Dir',	'ASC',			'word' );
		$search				= JRequest::getVar('resume_search');
		$search				= JString::strtolower( $search );        
				
		/* search filter */
		$lists['search']= $search;
		$item = new stdClass();
		
		foreach ($fieldList as $field){
			if($field->is_on_filter && ($field->field_type=='SELECT'||$field->field_type=='MSELECT')){
				$item->{$field->field_name} = $mainframe->getUserStateFromRequest( $option.".jaresumes.".$field->field_name,   $field->field_name,   '', 'string' );
				if ($field->field_type=='MSELECT')	$field->field_type = 'SELECT';
				$lists['filter'][$field->field_name] = rendfield($item, $field, true);
			}
		}

        // filter_state
        $lists['option']    = $option;
		
		return $lists;
	}

//    PhuongLH - Check Stared - Begin -

	function check_stared($id)
	{
		$user = & JFactory::getUser();
		$db = & JFactory::getDBO();
		$table_name = 'ja_resumes';
		$query = 'SELECT id FROM #__ja_shortlist WHERE (user_id='.$user->id.') and (listtype="'.$table_name.'") and (item_id='.$id.')';
		$db->setQuery($query);
		$items = $db->loadObjectList();
		if ($items)
			return true;
		return false;
		
	}

//    PhuongLH - Check Stared - End   -	
	
    function getToggleLink( $field_name, $row, $check=false, $imgY = 'Favorites-16x16.png', $imgX = 'favorite-remove-16x16.png', $prefix=''){
    	global $mainframe;
    	$img     = $check ? $imgY : $imgX;
        $task = $check ? "inactivate":"activate";
        $alt     = $check ? JText::_( 'REMOVE FAVORITE' ) : JText::_( 'SET AS FAVORITE' );
        $action  = $check ? JText::_( 'REMOVE FAVORITE' ) : JText::_( 'SET AS FAVORITE' );      

        $href = '
        <a id = "hrf_'.$row->id.'"  href="javascript:void(0);" onclick="return listItemTask(\''. $row->id .'\',\''. $field_name. '\',\''.$prefix.$task .'\')" title="'. $action .'">
        <img  id = "img_'.$row->id.'" src="'.JURI::base().'components/com_jajobboard/images/'. $img .'" border="0" alt="'. $alt .'" /></a>'
        ;

        return $href;
    }
    
   function multi_view()
   {
   		$user = JFactory::getUser();
    	$model = &$this->getModel();
    	$items = $model->geItemView_List();
    	$this->setLayout('jaview');
    	if (count($items)>0)
    		if (!checkPermission('can_search_resume'))
    		{
    			if (count($items)>1||$items[0]->user_id!=$user->id)
	    			checkPermission('view_resume_details');
    		}
        
        // Check if user has a resume
        $mail_postback_jobseeker = JRequest::getVar('mail_postback_jobseeker',false);
    	if ($mail_postback_jobseeker)
    	{
    		$mail_data = array();
	    	$mail_data['youremail'] = JRequest::getVar('youremail');
	    	$mail_data['friendemail'] = JRequest::getVar('friendemail');
	    	$mail_data['subject'] = JRequest::getVar('subject');
	    	$mail_data['message'] = JRequest::getVar('message');
    	}                      
    	$this->assignRef('mail', $mail_data);
        
    	if (count($items)>0)
    	{
    		$arr_tmp = array();
        	for ($i=0;$i<count($items);$i++)
        	if (isset($items[$i]))
        		$arr_tmp[] = $items[$i]->id;
        	$str_resume_id = implode(',',$arr_tmp);
        	$where = " and user_id='$user->id'";
        	if ($str_resume_id)
        		$where .= " and item_id IN ($str_resume_id)";
        	$notes = $model->getNoteForItemView($where);
        	$this->assignRef('notes', $notes);
    	}
    	$fieldList = $this->get('EditFieldList');
        $fromsearch = JRequest::getVar('fromsearch');
        
        $this->assignRef('items', $items);
        $this->assignRef('fieldlist', $fieldList);
        $this->assignRef('fromsearch', $fromsearch);
        /* Paging */
        if(count($items)>5){
	       	$pagination = &$this->get('Pagination');  
	       	$this->assignRef('pagination', $pagination);
        }
   }
   
   function filter_resume()
   {
		if (!checkPermission('can_search_resume'))
		checkPermission('view_list_resumes');		
         		
  		global $mainframe, $option, $jbconfig;
  		 // get data items
        $model = &$this->getModel();               
        
        $where_more = '';
        $user = JFactory::getUser();
        if($this->getLayout()=='jaemployerlist'){
        	
        	$db = JFactory::getDBO();
        	
        	$sql = "select item_id from #__ja_shortlist where user_id='$user->id' and listtype='ja_resumes'";
        	$db->setQuery($sql);
        	$result = $db->loadResultArray();
        	
        	if($result){
        		$items_id = implode(',', $result);
        		$where_more .= " and a.id in ($items_id)";
        	}
        	else 
        		$where_more .= " and a.id = 0";
        }
        //if() $where_more = " and a.user_id=".(int)$user_id;
        $where_more .= ' and a.published=1';
        $where_more .= ' and a.approved=1';
        
        $limitstart = JRequest::getInt('limitstart', 0);
		$limit		= $mainframe->getUserStateFromRequest( $option.'.jaresumes.limit',		'limit',	$jbconfig['posts']->get('display_num', 20),	'int' );
				
       	$items = $model->getItems($where_more, $limit, $limitstart); 
       	
       	$filter_location = JRequest::getVar('location_id');
    	if ($filter_location)
			$items = filterItemListViaFieldName($items,'location_id_tmp',getFullLocations($filter_location));
		
       	$filter_category = JRequest::getVar('cat_id');
    	if ($filter_category)
			$items = filterItemListViaFieldName($items,'category_tmp',getFullCategories($filter_category));
		
       	require_once (JPATH_SITE . DS . 'components' . DS . 'com_jajobboard' . DS . 'helper' . DS . 'japagination.php');
       	$pagination = new JAPagination ( count($items), $limitstart, $limit );
       	
       	
       	$item_notes = $model->getItemNotes($items);      	
       	
       	$fieldEditList = $model->getEditFieldList(' and f.frontend_hidden=0');
       	$fieldList = $model->getFieldList();

//		$pagination = &$this->get('Pagination');       
        
        $lists = $this->_getViewLists($fieldEditList);  
        
        $ordering = true;      
        $this->assignRef('items', $items);
        $this->assignRef('item_notes', $item_notes);
        $this->assignRef('pagination', $pagination);
        $this->assignRef('fieldlist', $fieldList);
        $this->assignRef('lists', $lists);
        $this->assignRef('ordering', $ordering );
   }
   function getListLimit($limit){
    	$array = array(5, 10, 15, 20, 50, 100);
    	$list_html = array();
    	foreach ($array as $value){
    		$list_html[] = JHTML::_('select.option', $value, $value);  
    	}
    	$list_html = JHTML::_('select.genericlist',   $list_html, 'limit', 'class="inputbox" size="1" onchange="doSearch(this.value)"', 'value', 'text', $limit);
    	return $list_html;
    }
   

}


?>